### TYPE LEVEL VARIABLES

# A) Project Categories

# Number of projects:

l_t <- L - rowSums(types[,2:(L+1)] == 0)

# Replace category codes in residual types:

types[ types == -1 ] <- NA

# Table of project categories for each network type:

Xp_t <- types[,2:ncol(types)]
Xp_t[types[,2:ncol(types)] == 0] <- NA

###

# B) Predicted Impact of Each Project

# Predicted impact for each project category:

temp1 <- tapply(Y[names(X_P_auth)], as.numeric(X_P_auth), mean)

# cbind(
#	tapply(Y[names(X_P_auth)], X_P_auth, min ), 
#	tapply(Y[names(X_P_auth)], X_P_auth, max ), temp1)
# (min, mean, max are the same)

# Table of predicted impacts for each network type:

Y_t <- matrix(temp1[Xp_t], nrow=nrow(Xp_t), ncol=ncol(Xp_t) )

# Replace NA's for isolated types:

Y_t[types[,"X1"]==0, 1] <- 0

###

# B) Number of Authors

# Number of authors for each project type:

temp2 <- tapply(num_authors_trunc[names(X_P_auth)], as.numeric(X_P_auth), mean)

# cbind(
#	tapply(articles[names(X_P_auth),"num_authors"], X_P_auth, min ), 
#	tapply(articles[names(X_P_auth),"num_authors"], X_P_auth, max ), temp2)
# (min, mean, max are the same)

# Table of number of authors for each network type:

N_t <- matrix(temp2[Xp_t], nrow=nrow(Xp_t), ncol=ncol(Xp_t) )

# Replace NA's for isolated types:

N_t[types[,"X1"]==0, 1] <- 0

###

# C) Unfamiliar Topic

# Indicators for unfamiliar topic for each project type:

temp3 <- as.logical( substr(levels(X_P_auth),3,3) )
	names(temp3) <- names(table(as.numeric(X_P_auth) ) )
	
# cbind(table(X_P_auth), temp3)
# (values match labels)

# Tables of indicators for unfamiliar ("dif"ferent) topics:

dif_t <- matrix(temp3[Xp_t], nrow=nrow(Xp_t), ncol=ncol(Xp_t) )

# Replace NA's for isolated types:

dif_t[types[,"X1"]==0, 1] <- FALSE

###

# D) Residual Types

if (any(is.na(types) ) )  {
	temp1 <- which(is.na(types[,2]) )
	temp1 <- temp1[(nrow(typesR1)+1):length(temp1)]
	temp2 <- sum(c(countsR2c, countsR2d) > 0)
	temp3 <- sum(c(countsR2e, countsR2f) > 0)
	  N_t[temp1[1:(nrow(typesR2a)+nrow(typesR2b))],1] <- 1
	  N_t[temp1[nrow(typesR2a)+nrow(typesR2b) + (1:temp2)],1] <- 3
	  N_t[temp1[nrow(typesR2a)+nrow(typesR2b) + temp2 + (1:temp3)],1] <- 4
	dif_t[temp1,1] <- FALSE
	dif_t[temp1[nrow(typesR2a) + 1:nrow(typesR2b)],1] <- TRUE
	dif_t[temp1[nrow(typesR2a)+nrow(typesR2b) + temp2-nrow(typesR2d) + 1:nrow(typesR2d)],1] <- TRUE
	dif_t[temp1[nrow(typesR2a)+nrow(typesR2b) + temp2 + temp3-nrow(typesR2f) + 1:nrow(typesR2f)],1] <- TRUE
	# Check first 99 rows:
	cbind(types,l_t,N_t,dif_t,counts)[1:99,]
}
